package com.pda.common.pages.codereset;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity;
import com.pda.common.bean.CodeDeatailBean;
import com.pda.common.beanin.CodeQueryInBean;
import com.pda.common.broadcast.NetWorkChangReceiver;
import com.pda.common.scan.BroadcastConfig;
import com.pda.common.tools.SharedPreferencesManger;
import com.pda.common.tools.StringProcess;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeResetActivity extends BaseActivity<CodeResetModel, CodeResetView, CodeResetPresent> implements CodeResetView {
    private String TAG = "CodeResetActivity";
    @BindView(R.id.tvss)
    TextView tvss;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.oet)
    EditText oet;
    @BindView(R.id.btproduce)
    Button btproduce;
    @BindView(R.id.tvs)
    TextView tvs;
    @BindView(R.id.tvwl)
    TextView tvwl;
    NetWorkChangReceiver netWorkChangReceiver;
    CodeQueryInBean codeQueryInBean = new CodeQueryInBean();

    @Override
    protected void initdata() {
        tvTitle.setText("数码查询");
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        registerReceiver(mScanfBroadcastReceiver, intentFilter);
        netWorkChangReceiver = new NetWorkChangReceiver(new NetWorkChangReceiver.PriorityListener() {
            @Override
            public void setText(String string) {
                tvss.setText(string);
            }
        });
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkChangReceiver, filter);
        codeQueryInBean.setAction("querycode");
        codeQueryInBean.setCid(SharedPreferencesManger.getCid());
        codeQueryInBean.setUserID(SharedPreferencesManger.getUID());
        codeQueryInBean.setStoreID(SharedPreferencesManger.getStoreID());
    }

    protected BroadcastReceiver mScanfBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                String code = StringProcess.codeCL(intent.getStringExtra("scannerdata"));
                if (StringProcess.boxcodeJudge(code) || StringProcess.casecodeJudge(code)) {
                    oet.setText(code);
                    codeQueryInBean.setCode(oet.getText().toString());
                    presenter.codeReset(codeQueryInBean);
                }
                else
                {
                    showMsg(code+" 条码长度不符");
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset);
        ButterKnife.bind(this);
        initdata();
//        oet.setText("2534096439429807");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(netWorkChangReceiver);
        unregisterReceiver(mScanfBroadcastReceiver);
    }

    @Override
    public CodeResetModel createModel() {
        return new CodeResetModelImpl(this);
    }

    @Override
    public CodeResetView createView() {
        return this;
    }

    @Override
    public CodeResetPresent createPresenter() {
        return new CodeResetPresent();
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvs.setText(str);
            }
        });
    }

    @Override
    public void tipwl(final List<CodeDeatailBean.DataBean.ListWLBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String str="物流信息:";
                if(list==null||list.size()==0)
                {
                    str=str+"\n暂无";
                }
                else
                {
                    for (int i = 0; i <list.size() ; i++) {
                        str=str+"\n"+"单号:"+list.get(i).getBillCode()+"；\n操作时间:"+list.get(i).getDotime()+"；\n发货方:"+list.get(i).getStorename()+"；\n收货方:"+list.get(i).getDealername()+"。\n";
                    }
                }
                tvwl.setText(str);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.btproduce})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.btproduce:
                if (StringProcess.boxcodeJudge(oet.getText().toString()) || StringProcess.casecodeJudge(oet.getText().toString())) {
                    codeQueryInBean.setCode(oet.getText().toString());
                    presenter.codeReset(codeQueryInBean);
                } else {
                    showMsg(oet.getText().toString()+" 条码长度不符");
                }
                break;
        }
    }
}
